package fi.hell.pollit.domain

class PollController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [pollInstanceList: Poll.list(params), pollInstanceTotal: Poll.count()]
    }

    def create = {
        def pollInstance = new Poll()
        pollInstance.properties = params
        return [pollInstance: pollInstance]
    }

    def save = {
        def pollInstance = new Poll(params);
        pollInstance.creator = User.getLoggedInUser();
        pollInstance.validate();

        List<String> options = params.list("optionNames");
        for (String option in options) {
            Opt o = Opt.findByName(option);
            if (!o) {
                o = new Opt(["name":option]);
                if (!o.save(flush:true)) {
                    pollInstance.errors.rejectValue("options", o.errors.getFieldError("name").rejectedValue);
                } else {
                    pollInstance.addToOptions(o);
                }
            } else {
                pollInstance.addToOptions(o);
            }
        }

        if (pollInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'poll.label', default: 'Poll'), pollInstance.id])}"
            redirect(action: "show", id: pollInstance.id)
        }
        else {
            int optionsSize = params.list('options')?.size();
            render(view: "create", model: [pollInstance: pollInstance, "optionsSize":optionsSize])
        }
    }

    def show = {
        def pollInstance = Poll.get(params.id)
        if (!pollInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'poll.label', default: 'Poll'), params.id])}"
            redirect(action: "list")
        }
        else {
            [pollInstance: pollInstance]
        }
    }

    def edit = {
        def pollInstance = Poll.get(params.id)
        if (!pollInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'poll.label', default: 'Poll'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [pollInstance: pollInstance]
        }
    }

    def update = {
        def pollInstance = Poll.get(params.id)
        pollInstance.validate();

        List<Opt> currentOptions = pollInstance.options as List;
        for (Opt o in currentOptions) {
            pollInstance.removeFromOptions(o);
        }

        List<String> options = params.list("optionNames");
        for (String option in options) {
            Opt o = Opt.findByName(option);
            if (!o) {
                o = new Opt(["name":option]);
                if (!o.save(flush:true)) {
                    pollInstance.errors.rejectValue("options", o.errors.getFieldError("name").rejectedValue);
                } else {
                    pollInstance.addToOptions(o);
                }
            } else {
                pollInstance.addToOptions(o);
            }
        }

        if (pollInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (pollInstance.version > version) {
                    
                    pollInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'poll.label', default: 'Poll')] as Object[], "Another user has updated this Poll while you were editing")
                    render(view: "edit", model: [pollInstance: pollInstance])
                    return
                }
            }
            pollInstance.properties = params

            if (!pollInstance.hasErrors() && pollInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'poll.label', default: 'Poll'), pollInstance.id])}"
                redirect(action: "show", id: pollInstance.id)
            }
            else {
                render(view: "edit", model: [pollInstance: pollInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'poll.label', default: 'Poll'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def pollInstance = Poll.get(params.id)
        if (pollInstance) {
            try {
                pollInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'poll.label', default: 'Poll'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'poll.label', default: 'Poll'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'poll.label', default: 'Poll'), params.id])}"
            redirect(action: "list")
        }
    }
}
